package com.zzyl.nursing.controller;

import java.util.List;
import java.util.Map;

import com.huaweicloud.sdk.iotda.v5.model.ServiceCapability;
import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.serve.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备Controller
 * 
 * @author BIG
 * @date 2025-06-08
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备相关接口")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取设备列表")
    public TableDataInfo<List<Device>> list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }


    @PostMapping("/syncProductList")
    @ApiOperation(value = "同步产品列表")
    public AjaxResult syncProductList(){
        deviceService.syncProductList();
        return success();
    }

    @GetMapping("/allProduct")
    @ApiOperation(value = "获取所有产品")
    public R<List<ProductVo>> allProduct(){
        List<ProductVo> list = deviceService.allProduct();
        return R.ok(list);
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册设备")
    public AjaxResult register(@RequestBody DeviceDto deviceDto){
        deviceService.register(deviceDto);
        return success();
    }

    /**
     * 获取设备详细信息
     */
    @GetMapping("/{iotId}")
    @ApiOperation(value = "获取设备信息")
    public R<DeviceDetailVo> selectDeviceByIodId(@PathVariable String iotId){
        DeviceDetailVo deviceDetailVo = deviceService.selectDeviceByIodId(iotId);
        return R.ok(deviceDetailVo);
    }

    /**
     * 修改设备
     */
    @PutMapping()
    @ApiOperation(value = "修改设备")
    public AjaxResult updateDevice( @RequestBody DeviceDto deviceDto){
        deviceService.updateIotDevice(deviceDto);
        return success();
    }


    /**
     * 删除设备
     */
    @DeleteMapping("/{iotId}")
    @ApiOperation(value = "删除设备")
    public AjaxResult removeDevice( @PathVariable String iotId){
        deviceService.removeDevice(iotId);
        return success();
    }

    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation(value = "查询设备服务属性")
    public R<List<Map<String, Object>>> queryServiceProperties(@PathVariable String iotId){
        List<Map<String, Object>> list = deviceService.queryServiceProperties(iotId);
        return R.ok(list);
    }

    /**
     * 查询产品详情
     */

    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation(value = "查询iot产品详情")
    public AjaxResult queryProduct(@PathVariable String productKey){
        List<ServiceCapability> serviceCapabilities = deviceService.queryProduct(productKey);
        return success(serviceCapabilities);
    }
}
